﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace Mirkuz.UI
{
    public class TButton : TLabel
    {        
        bool _focused = false;
        bool _pressed = false;

        public TButton() :base()
        {
            this.GotFocus += new EventHandler(TButton_GotFocus);
            this.LostFocus += new EventHandler(TButton_LostFocus);
            this.MouseDown += new MouseEventHandler(TButton_MouseDown);
            this.MouseUp += new MouseEventHandler(TButton_MouseUp);
        }

        public Image FocusedImage { get; set; }
        public Image PressedImage { get; set; }
        public Image NormalImage { get; set; }

        void TButton_MouseDown(object sender, MouseEventArgs e)
        {
            _pressed = true;
            this.Invalidate();
        }

        void TButton_MouseUp(object sender, MouseEventArgs e)
        {
            _pressed = false;
            this.Invalidate();
        }

        void TButton_LostFocus(object sender, EventArgs e)
        {
            _focused = false;
            this.Invalidate();
        }

        void TButton_GotFocus(object sender, EventArgs e)
        {
            _focused = true;
            this.Invalidate();
        }       

        protected override void OnPaint(PaintEventArgs pe)
        {
            if (NormalImage != null)
            {
                Rectangle src = new Rectangle(0, 0, NormalImage.Width, NormalImage.Height);
                Rectangle dst = new Rectangle(0, 0, Bounds.Width, Bounds.Height);
                //image
                if (_pressed)
                {
                    if (PressedImage != null)
                        pe.Graphics.DrawImage(PressedImage, dst, src, GraphicsUnit.Pixel);
                }
                else
                {
                    if (_focused)
                    {
                        if (FocusedImage != null)
                            pe.Graphics.DrawImage(FocusedImage, dst, src, GraphicsUnit.Pixel);
                    }
                    else
                    {
                        if (NormalImage != null)
                            pe.Graphics.DrawImage(NormalImage, dst, src, GraphicsUnit.Pixel);
                    }
                }
            }
            base.OnPaint(pe);
        }
    }
}
